package com.feicheng.lanniao.controller.pc;

import com.feicheng.lanniao.entity.CarImage;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.CarImageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author tianchunlei
 * @Des //用户前台pc上传小图片接口
 * @Date 2021/9/26
 **/
@RestController()
@RequestMapping("/lanniao-user/car/")
public class CarImagePcController {

    @Resource
    private CarImageService carImageService;

    /**
     * @Author tianchunlei
     * @Description //批量上传小图接口
     * @Date 2021/9/26 15:33
     * @Param images 多张图片拼接的地址 carId，关联的车辆id
     * @return
     */
    @PutMapping("/image/upload")
    public ResultInfo carImageUpload(Integer carId,String images){
        System.out.println(images);
        //解析多张图片的字符串
        String[] imageUrls = images.split(",");
        List<CarImage> carImageUrlList = new ArrayList<>();
        for (String carImageUrl : imageUrls) {
            CarImage carImage = new CarImage();
            carImage.setCarId(carId);
            carImage.setImgUrl(carImageUrl);
            carImageUrlList.add(carImage);
        }
        return carImageService.batchAdd(carImageUrlList);
    }

    /**
     * @Author tianchunlei
     * @Description //批量删除小图片地址的接口
     * @Date 2021/9/26 16:48
     * @Param carId
     * @return
     */
    @DeleteMapping("/image/remove/{carId}")
    public ResultInfo deleteImageUrl(@PathVariable("carId") Integer carId){
        return carImageService.batchDelete(carId);
    }

    /**
     * @Author tianchunlei
     * @Description //按照车辆编号查询车辆小图集合
     * @Date 2021/9/26 16:57
     * @Param carId
     * @return
     */
    @GetMapping("/image/list/{carId}")
    public ResultInfo list(@PathVariable("carId") Integer carId){
        return carImageService.selectUrlAll(carId);
    }
}
